﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class DemoGridView_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadManufacturer();
    }

    private void LoadManufacturer()
    {
        SqlDataAdapter da = new SqlDataAdapter("select * from Manufacturer", DataAccess.ConnectionString);
        DataSet ds = new DataSet();
        da.Fill(ds);
        grvM.DataSource = ds.Tables[0];
        grvM.DataBind();
    }
    protected void grvM_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvM.EditIndex = e.NewEditIndex;
        LoadManufacturer();
    }
    protected void grvM_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvM.EditIndex =-1;
        LoadManufacturer();
    }
    protected void grvM_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //get id
        string id = grvM.DataKeys[e.RowIndex].Value.ToString();
        //get value of control
       // string name1 = ((TextBox)grvM.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string name = ((TextBox)grvM.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

        string address = ((TextBox)grvM.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        string email = ((TextBox)grvM.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
        string phone = ((TextBox)grvM.Rows[e.RowIndex].Cells[5].Controls[0]).Text;

        string describe = ((TextBox)grvM.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
        string logo = ((TextBox)grvM.Rows[e.RowIndex].Cells[7].Controls[0]).Text;
        string lang = ((TextBox)grvM.Rows[e.RowIndex].Cells[8].Controls[0]).Text;
        string active = ((TextBox)grvM.Rows[e.RowIndex].Cells[9].Controls[0]).Text;
        //update
        SqlCommand cmd = DataAccess.Connection.CreateCommand();
        cmd.CommandText = "update manufacturer set mname=@name,address=@address,@email=@email,phone=@phone,describe=@describe,@lang=lang,@logo=logo,active=@active where mid=@mid";
        //add value to parameter
        cmd.Parameters.AddWithValue("@name",name);
        cmd.Parameters.AddWithValue("@Address",address);
        cmd.Parameters.AddWithValue("@email", email);
        cmd.Parameters.AddWithValue("@phone", phone);
        cmd.Parameters.AddWithValue("@describe", describe);
        cmd.Parameters.AddWithValue("@logo", logo);
        cmd.Parameters.AddWithValue("@lang",lang);
        cmd.Parameters.AddWithValue("@active", active);
        cmd.Parameters.AddWithValue("@mid", id);
        cmd.ExecuteNonQuery();
        //reload
        grvM.EditIndex = -1;
        LoadManufacturer();
    }
    protected void grvM_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex < 0) return;
        if (grvM.EditIndex != e.Row.RowIndex)
        {
            //search linkbutton delete for add confirm OK, Cancel
            LinkButton btndel=(LinkButton)e.Row.Cells[0].Controls[2];
            btndel.Attributes.Add("onclick", "return confirm('Do you want delete this row?')");
        }
    }
    protected void grvM_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //get id
        string id = grvM.DataKeys[e.RowIndex].Value.ToString();
        SqlCommand cmd = DataAccess.Connection.CreateCommand();
        cmd.CommandText = "delete from manufacturer where mid=@mid";
        cmd.Parameters.AddWithValue("@mId", id);
        cmd.ExecuteNonQuery();
        //reload
        LoadManufacturer();
    }
}
